package cn.aeolia.dao;

import cn.aeolia.pojo.Account;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Repository
public class AccountDao {

    private static Map<Integer, Account> accountMap = null;

    /**
     * 模拟数据库数据
     */
    static {
        accountMap=new HashMap<>();

        accountMap.put(2001,new Account(2001,"admin","admin123"));
    }

    /**
     * 模拟主键自增长
     */
    private static Integer initID=2002;

    /**
     * 根据id获取account
     * @param id
     * @return
     */
    public Account getAccountById(Integer id){
        return accountMap.get(id);
    }

    /**
     * 添加account
     * @param account
     */
    public void addAccount(Account account){
        accountMap.put(initID++,account);
    }

    /**
     * 根据username和password获取account
     * @param account
     * @return
     */
    public Account getAccountByUP(Account account){
        for (Integer integer : accountMap.keySet()) {
            Account accountOut = accountMap.get(integer);
            account.setAid(integer);
            if (accountOut.equals(account)){
                return accountOut;
            }
        }
        return null;
    }

    /**
     * 获取所有员工信息
     * @return
     */
    public List<Account> getAllAccount(){
        Set<Integer> keySet = accountMap.keySet();
        List<Account> accounts=null;
        for (Integer key : keySet) {
            accounts.add(accountMap.get(key));
        }
        return accounts;
    }

    /**
     * 添加一群员工
     * @param accounts
     */
    public void addAccounts(List<Account> accounts){
        for (Account account : accounts) {
            addAccount(account);
        }
    }
}
